package com.sinaapp.mspring.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.echarts.data.WordCloudData;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.WordCloud;
import com.github.abel533.echarts.style.ItemStyle;
import com.github.abel533.echarts.style.itemstyle.Normal;
import com.sinaapp.mspring.dao.TmallMobileMapper;
import com.sinaapp.mspring.service.TmallMobileServiceI;
import com.sinaapp.mspring.vo.TMobile;

@Service
public class TmallMobileServiceImpl implements TmallMobileServiceI {

	@Autowired
	private TmallMobileMapper tmallDao;
	
	@Override
	public String wordCloud() {
		List<TMobile> tMobiles = tmallDao.wordCloud();
		List<WordCloudData> cloudDatas = new ArrayList<>();
		for(TMobile r : tMobiles) {
			WordCloudData w = new WordCloudData(r.getBrand(), r.getCount().intValue())
					.itemStyle(new ItemStyle().normal(createRandomItemStyle()));
			cloudDatas.add(w);
		}
		GsonOption option = new GsonOption();
		option.title().text("天猫手机销量数据");
        option.tooltip().show(true).formatter("{a} <br/>{b} : {c} 台");
        WordCloud wordCloud = new WordCloud("手机销量");
        wordCloud.size("80%", "80%");
        wordCloud.textRotation(0, 45, 90, -45);
        wordCloud.textPadding(0);
        wordCloud.autoSize().enable(true).minSize(14);
        wordCloud.data(cloudDatas.toArray());
        
        option.series(wordCloud);
		return option.toString();
	}

	/**
	 * 生成随机色
	 * @return
	 */
	private Normal createRandomItemStyle() {
        Normal normal = new Normal();
        normal.color("rgb(" + Math.round(Math.random() * 160) + ","
                + Math.round(Math.random() * 160) + ","
                + Math.round(Math.random() * 160) + ")");
        return normal;
    }

	@Override
	public List<TMobile> top10() {
		return tmallDao.top10();
	}
}
